﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Dataglot;

namespace IrishSettings.Commands
{
    public class SelectApplicationByName:GenericCommand
    {
        public SelectApplicationByName(DataFactory factory)
            : base(factory)
        {
            Construct();
        }

        private void Construct()
        {
            const string sql =
                @"
SELECT     * 
FROM         {$ns}_settings_applications 
WHERE 
    {$ns}_settings_applications.application= @application
    AND {$ns}_settings_applications.version= @version ";
            ComposeSql(sql);
            AddParameter("@application", DbType.String);
            AddParameter("@version", DbType.String);
        }

        public SelectApplicationByName(DataFactory factory, DbConnection shared, DbTransaction transaction)
            : base(factory, shared, transaction)
        {
            Construct();
        }

        public DbDataReader SelectOne(
            string application
            ,Version version
            )
        {
            if (string.IsNullOrEmpty(application))
                throw new ArgumentException("Missing application name", "application");
            if (version == null)
                throw new ArgumentException("Missing version", "version");

            command.Parameters["@application"].Value = application;
            command.Parameters["@version"].Value = version.ToString();
            return ExecuteReader(CommandBehavior.SingleRow);
        }
    }
}
